<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
      <BasicForm @register="registerForm"/>
      <!--- 添加页脚 --->
      <template #footer>  
        <a-button type="primary" @click="handlePass" shape="round">通过</a-button>
        <a-button type="danger"  @click="handleReject" shape="round">不通过</a-button>
      </template>
  </BasicModal>
    <!--- 输入申请理由的弹窗 --->
  <BasicModal destroyOnClose @register="registerConfirmReason" title="请输入审批批注">
    <a-textarea v-model:value="scheduleReviewMsg" show-count :rows="8" :maxlength="256" />
    <template #footer>
        <a-button type="primary" @click="handleOk" :loading="iconLoading">审批完成</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner, useModal} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../VmSchedule.data';
    import {saveOrUpdate, schedulePass, scheduleReject} from '../VmSchedule.api';

    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });

    const scheduleReviewMsg = ref<string>('');
    const iconLoading = ref<boolean>(false);
    const ifPass = ref<boolean>(false);
    const [registerConfirmReason,registerConfirmReasonMethods ] = useModal();

    async function handleOk() {
        iconLoading.value = true;
        const data = getFieldsValue();
        if (ifPass.value) {
            await schedulePass({id:data.id,reviewMsg:scheduleReviewMsg.value}, () => {iconLoading.value = false;emit('success');closeModal();})
        } else {
            await scheduleReject({id:data.id,reviewMsg:scheduleReviewMsg.value}, () => {iconLoading.value = false;emit('success');closeModal();})
        }
        registerConfirmReasonMethods.closeModal();
        closeModal();
    }
    
    function handlePass() {
        ifPass.value = true;   
        scheduleReviewMsg.value = "审批通过"
        registerConfirmReasonMethods.openModal();
    }

    async function handleReject() {
        ifPass.value = false; 
        scheduleReviewMsg.value = "审批不通过"
        registerConfirmReasonMethods.openModal();
    }

    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate,getFieldsValue}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 24}
    });


    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '编辑' : '审批'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            console.log(v)
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>